/**
 * Copyright (C), 2015-2019, 鼎一致远科技发展有限公司
 * FileName: FileUpload
 * Author:   Vince-ZZ
 * Date:     2019/9/4 0004 下午 2:14
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.study.controller;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/file")
public class FileUpload {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileUpload.class);

    @Value("${upload.path}")
    private String uploadPath;
    @PostMapping("/upload")
    //单文件上传
    public Boolean upload(@RequestParam("file") MultipartFile file) {
        BufferedOutputStream stream = null;
        try {
            if (file.isEmpty()) {
                LOGGER.info("文件为空：");
                return false;
            }
            // 获取文件名
            String fileName = file.getOriginalFilename();
            LOGGER.info("上传的文件名为：" + fileName);
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            LOGGER.info("文件的后缀名为：" + suffixName);

            File dest = new File(uploadPath + fileName);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            file.transferTo(dest);
            LOGGER.info("文件的上传路径：" + uploadPath);
            return true;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    //多文件上传
    @PostMapping("/uploadMore")
    public boolean handleFileUpload(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        MultipartFile file = null;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            if (!file.isEmpty()) {
                try {
                    String fileName = file.getOriginalFilename();
                    File dest = new File(uploadPath + fileName);
                    file.transferTo(dest);
                } catch (Exception e) {
                    LOGGER.info("第{0}个文件上传失败 ==>{1} ",i,e.getMessage());
                    return false;
                }
            } else {
                LOGGER.info("第{0}个文件为空，上传失败 ",i);
                return false;
            }
        }
        return true;
    }
}
